package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户资源控制器
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private IStrategyCommentService commentService;

    @GetMapping
    public PageInfo<Strategy> list(StrategyQueryObject qo){
        return strategyService.list(qo);
    }

    //根据大攻略信息id获取大攻略信息
    @GetMapping("{strategyId}")
    public Strategy getStrategyById(@PathVariable Long strategyId){
        return strategyService.selectById(strategyId);
    }


    //根据大攻略id获取分类的集合
    @GetMapping("/{strategyId}/catalogs")
    public List<StrategyCatalog> queryStrategyCatalogs(@PathVariable Long strategyId){
        return  catalogService.listByStrategyId(strategyId);
    }


    //根据大攻略id获取评论信息
    @GetMapping("/{strategyId}/comments")
    public PageInfo<StrategyComment> queryStrategyCommentById(@ModelAttribute("qo") StrategyCommentQueryObject qo,Model model){
        qo.setOrderBy("sc.createTime desc");
        return  commentService.list(qo);
    }

    //根据大攻略id获取评论信息
    @PostMapping("/{strategy.id}/comments")//获取攻略的id,直接写的话,方便封装,不然要用一个变量接收该id
    public JSONResult save(StrategyComment comment,String[] tags){
        //设置当前登录用户的id
        comment.setUser(UserContext.getCurrentUser());
        commentService.saveOrUpdate(comment,tags);
        return new JSONResult();
    }


    //根据大攻略id获取标签
    @GetMapping("/{strategyId}/tags")
    public List<String> queryTagsByStrategyId(@PathVariable Long strategyId,Integer size){
        return  commentService.queryTagsByStrategyId(strategyId,size);
    }
}
